import os, sys
import cv2
import time
import argparse
from pathlib2 import Path
from global_var import globalVars
# from GUI import gui_operate   # GUI后由C#实现，但也可以用PYQT5实现
from License_Plate_Chars_Recognize import lpcr_operate
from License_Plate_Color_Recognize import lpcor_operate
from License_Plate_Localization import lpl_operate
from License_Plate_Localization import make_data
from Image_Enhancement import ie_operate
from Optical_Char_Recognize import ocr_operate


time00 = time.time()
# 处理命令行传入相关指令和图片地址
# e.g. python main.py --img_process --file xxx.jpg 详见ReadME.md
parser = argparse.ArgumentParser()
parser.description = "GD python part"
parser.prog = "GD_Python"
parser.add_argument('--make_data', action='store_true',
                    default=False,
                    dest='boolean_make_data',
                    help='Switch to make data')
parser.add_argument('--img_process', action='store_true',
                    default=False,
                    dest='boolean_img_process',
                    help='Switch to image process')
parser.add_argument('--measure_index', action='store_true',
                    default=False,
                    dest='boolean_measure_index',
                    help='Switch to measure index')
parser.add_argument('--file', action='store',
                    type=str,
                    default='',
                    dest='file',
                    help='Input file path')
parser.add_argument('--folder', action='store',
                    type=str,
                    default='',
                    dest='folder',
                    help='Input folder path')
parser.add_argument('--version', action='version',
                    version='%(prog)s 1.0')

arg = parser.parse_args()


# 用于进行图像处理
def ImgProcess(imgPath):
    outPath = globalVars.projectPath / Path('output')
    defaultPath = outPath / Path('defaultPicture.jpg')
    plateImgWholePath = outPath / Path('lpl', 'plateImg_whole.jpg')
    plateImgPrecisePath = outPath / Path('lpl', 'plateImg_precise.jpg')
    plateImgGeneralPath = outPath / Path('lpl', 'plateImg_general.jpg')
    resultTxtPath = outPath / Path('result.txt')
    txtData = []
    img = cv2.imread(imgPath.__str__())
    fileName = imgPath.stem

    plateImg_whole, plateImg_general, plateImg_precise, plateLocateConf = lpl_operate.Lpl_Operator(img, fileName)
    refinedChars, plateOcrScore = ocr_operate.Ocr_Operator(plateImg_precise, fileName)
    plateStringNumber, plateRecognizeConf = lpcr_operate.Lpcr_Operator(refinedChars, plateImg_precise)
    color = lpcor_operate.Lpcor_Operator(plateImg_precise, fileName)

    print(color, plateStringNumber, plateRecognizeConf)

    for i, pic in enumerate(refinedChars):
        fullFilePath = outPath / Path('ocr', f"{i}.jpg")
        cv2.imwrite(fullFilePath.__str__(), pic)
    cv2.imwrite(plateImgWholePath.__str__(), plateImg_whole)
    cv2.imwrite(plateImgPrecisePath.__str__(), plateImg_precise)
    cv2.imwrite(plateImgGeneralPath.__str__(), plateImg_general)

    time01 = time.time()
    txtData.append(plateStringNumber + '\n')
    txtData.append(color + '\n')
    txtData.append(str(plateRecognizeConf) + '\n')
    txtData.append(str(time01-time00) + '\n')

    with open(resultTxtPath.__str__(), "w+", encoding='utf-8') as f:
        f.writelines(txtData)


# 用于进行数据集的标签制作
# TODO:只对lpl模块编写了数据集标签制作的函数,可以整合其他模块制作标签的函数
def MakeData():
    lpl_operate.make_data.CreateLabelTxt()
    pass


# 用于对任意模块进行指标测试
# TODO:可以自由编写
def MeasureIndex(imgPath):
    pass


if arg.boolean_make_data:
    MakeData()
if arg.boolean_img_process:
    ImgProcess(Path(arg.file))
if arg.boolean_measure_index:
    MeasureIndex()